import React, { Component } from 'react';
import PropTypes from 'prop-types'
import { nanoid } from 'nanoid';
import header from './index.module.css';

export default class Header extends Component {
  static propTypes = {
    addTodo: PropTypes.func.isRequired
  }

  handleKeyUp = e => {
    const { keyCode, target } = e;
    if (Number(keyCode) !== 13) return;
    if (target.value.trim() === '') {
      alert('不能添加空');
      return;
    }
    // 准备传递对象
    const todoObj = { id: nanoid(), name: target.value, done: false };
    this.props.addTodo(todoObj);
    target.value = '';
  };
  
  render() {
    return (
      <div className={header.headerbox}>
        <input onKeyUp={this.handleKeyUp} className={header.playinput} type="text" placeholder="请输入你的任务名称，按回车确认2" />
      </div>
    );
  }
}
